package com.lanyou.cook.entitylistener.domain;

import java.util.HashSet;
import java.util.List;

import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.datadictionary.UserLogOpt;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.repository.userManagement.UserRepository;
import com.lanyou.cook.service.ApplicationContextKeeper;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.esb.redis.proxy.impl.FieldSystemJedisProxy;
import com.lanyou.esb.redis.vo.FieldSystemVo;

public class FieldSystemEntityListenerForLog {

	@PrePersist
	public void prePersist(FieldSystem target) {
		String content = String.format("新增领域系统“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceSystem, null,
				content);

		UserRepository userRepository = ApplicationContextKeeper
				.getApplicationContext().getBean(UserRepository.class);
		List<User> users = userRepository
				.findByRole_Id(Constants.FIXED_ROLE_ID_FOR_ADMIN);
		List<User> users2 = userRepository
				.findByRole_Id(Constants.FIXED_ROLE_ID_FOR_IF_ADMIN);
		if(!users2.isEmpty()){
			users.addAll(users2);
		}
		target.setUsers(new HashSet<>(users));
	}

	@PreUpdate
	public void preUpdate(FieldSystem target) {
		String content = String.format("修改领域系统“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceSystem, null,
				content);
	}

	@PreRemove
	public void preRemove(FieldSystem target) {
		String content = String.format("删除领域系统“%s”", target.getName());
		UserLogService.getInstance().save(UserLogOpt.MaintenanceSystem, null,
				content);
	}

	@PostPersist
	public void posPersist(FieldSystem target) {
		FieldSystemJedisProxy fieldSystemJedisProxy = new FieldSystemJedisProxy();
		try {
			FieldSystemVo tartet = FieldSystemVo.getInstance(target);
			fieldSystemJedisProxy
					.hset(fieldSystemJedisProxy.generateKeyFromObject(tartet),
							tartet);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}

	@PostUpdate
	public void posUpate(FieldSystem target) {
		FieldSystemJedisProxy fieldSystemJedisProxy = new FieldSystemJedisProxy();
		try {
			FieldSystemVo tartet = FieldSystemVo.getInstance(target);
			fieldSystemJedisProxy
					.hset(fieldSystemJedisProxy.generateKeyFromObject(tartet),
							tartet);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}

	@PostRemove
	public void posRemove(FieldSystem target) {
		FieldSystemJedisProxy fieldSystemJedisProxy = new FieldSystemJedisProxy();
		FieldSystemVo tartet = FieldSystemVo.getInstance(target);
		fieldSystemJedisProxy.hdel(tartet);
	}

}
